﻿using RUC.Core.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyBasicLib.Data.ORMLiteNew;
using System.Configuration;
using System.Data.SqlClient;
using Dapper;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;

namespace MyBasicLib.PerformanceTest
{
    class DataTest
    {
        static string id = "09405A2F-90B5-4391-8914-E153AB45E632";
        public static void Run(int count)
        {
            DataTest test = new DataTest();
            Diagnostics.CodeTimer.Initialize();
            test.ADOTest(count);
            test.DCTest(count);
            test.DapperTest(count);
            test.EFTest(count);
        }

        private void DCTest(int count)
        {
            var dc = new Data.ORMLiteNew.DataContext();
            //dc.GetTable<AppUser>().Compily.Single(p => p.Id == id).

            MyBasicLib.Diagnostics.CodeTimer.Time("DCTest", 1, () => dc.GetTable<AppUser>().Single(p => p.Id == id));
            MyBasicLib.Diagnostics.CodeTimer.Time("DCTest", count, () => dc.GetTable<AppUser>().Single(p => p.Id == id));
        }
        private void EFTest(int count)
        {

         
            var ef = new EFContext(ConfigurationManager.ConnectionStrings[Program.connectionStrings].ConnectionString);
           // Func<ObjectContext, AppUser> func = CompiledQuery.Compile<ObjectContext, AppUser>(c => c.AppUsers.Single(p => p.Id == id));
            //DbCompiledModel mm = new DbCompiledModel();
            //mm.CreateObjectContext<EFContext>(null);
            //MyBasicLib.Diagnostics.CodeTimer.Time("EFTest", 1, () => ef.AppUsers.Single(p => p.Id == id));
            //MyBasicLib.Diagnostics.CodeTimer.Time("EFTest", count, () => ef.AppUsers.Single(p => p.Id == id));
        }
        private void DapperTest(int count)
        {
            Action action = () =>
            {
                using (var conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings[Program.connectionStrings].ConnectionString))
                {
                    conn.Query<AppUser>("SELECT * FROM APPUSER WHERE ID = @p0", new { p0 = id });
                };
            };
            MyBasicLib.Diagnostics.CodeTimer.Time("DapperTest", 1, action);
            MyBasicLib.Diagnostics.CodeTimer.Time("DapperTest", count, action);


        }

        private void ADOTest(int count)
        {

            Action action = () =>
            {
                using (var conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings[Program.connectionStrings].ConnectionString))
                {
                    var cmd = conn.CreateCommand();
                    cmd.CommandText = "SELECT * FROM APPUSER WHERE ID = @p0";
                    cmd.Parameters.Add(new SqlParameter("@p0", id));

                    conn.Open();
                    using (var reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            object[] data = new object[reader.FieldCount];
                            reader.GetValues(data);
                        }
                    }
                };
            };

            MyBasicLib.Diagnostics.CodeTimer.Time("ADOTest", 1, action);
            MyBasicLib.Diagnostics.CodeTimer.Time("ADOTest", count, action);



        }
        public class EFContext : DbContext
        {
            public EFContext(String connection) : base(connection) { }
            public DbSet<AppUser> AppUsers { get; set; }
        }
    }
}
